package test01.demo02.userOperation.jdbc;

import test01.demo02.userOperation.UserIfo.User;
import test01.demo02.userOperation.tools.Const;

import java.sql.*;


public class UserJDBC {

    static {
        try {
            Class.forName(Const.DRIVER);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    public User selectUser(String userName){
        return selectUser(userName,null);
    }

    /**
     * 通过用户名查找用户
     * @param userName
     */
    public User selectUser(String userName,String pwd){

        User user = null;

        try(
                Connection conn = DriverManager.getConnection(
                        Const.URL_TEST,
                        Const.USERNAME,
                        Const.PASSWORD
                );
                Statement statement = conn.createStatement();
                ) {

            String sql = "select * from tb_user where " +
                    "userName = '"+userName+"'";
            //如果密码不为空，加密码的条件，这里的 "" 根据实际情况决定是否添加
            if(pwd!=null){
                sql += " and pwd='"+pwd+"'";
            }

            //将查询结果存储在rs中
            ResultSet rs = statement.executeQuery(sql);
            //
            if (rs.next()){
                user = new User(
                        rs.getInt(1),
                        rs.getString(2),
                        rs.getString(3),
                        rs.getString(4)
                );
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return user;
    }

    public int addUser(User user){

        int result = 0;

        try(
                Connection conn = DriverManager.getConnection(
                Const.URL_TEST,
                Const.USERNAME,
                Const.PASSWORD
        );
            Statement statement = conn.createStatement();
        ){
            String sql = "insert into tb_user values(" +
                    user.getid()+"," +
                    "'"+user.getUserName()+"'," +
                    "'"+user.getPwd()+"'," +
                    "'"+user.getEmail()+"'" +
                    ")";
            System.out.println(sql+"=====");
            result = statement.executeUpdate(sql);
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    public int updateEmail(String email,String userName){
        int result = 0;
        try(
                Connection conn = DriverManager.getConnection(
                        Const.URL_TEST,
                        Const.USERNAME,
                        Const.PASSWORD
                );
                Statement statement = conn.createStatement();
        ){
            String sql = "update tb_user set email='"+email+"' where userName = '"+userName+"'";

            result = statement.executeUpdate(sql);
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    public int deleteInfo(String userName){
        int result = 0;
        try(
                Connection conn = DriverManager.getConnection(
                        Const.URL_TEST,
                        Const.USERNAME,
                        Const.PASSWORD
                );
                Statement statement = conn.createStatement();
        ){
            String sql = "delete from tb_user where userName = '"+userName+"'";

            result = statement.executeUpdate(sql);
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    public void selectInfo(String userName) throws Exception {
        ResultSet result = null;
        Connection conn = null;
        Statement statement = null;
        try{
            conn = DriverManager.getConnection(
                    Const.URL_TEST,
                    Const.USERNAME,
                    Const.PASSWORD);
            statement = conn.createStatement();
            String sql = "select * from tb_user where userName = '"+userName+"'";

            result = statement.executeQuery(sql);
            while (true){
                if (result.next()){
                    System.out.println(result.getInt(1));
                    System.out.println(result.getString(2));
                    System.out.println(result.getString(3));
                    System.out.println(result.getString(4));
                }
                break;
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            result.close();
            conn.close();
            statement.close();
        }

    }


}
